package com.pinusmusic.dao.impl;

import com.pinusmusic.dao.AdminDao;
import com.pinusmusic.domain.Admin;
import com.pinusmusic.domain.Favorite;
import com.pinusmusic.domain.User;
import com.pinusmusic.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class AdminDaoImpl implements AdminDao {
    private JdbcTemplate jt = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public boolean findAdminByNameAndPassword(String adminname, String adminpassword) {
        String sql = "select * from admin where adminname = ? and adminpassword = ?";
        List<Admin> list = jt.query(sql, new BeanPropertyRowMapper<>(Admin.class), adminname, adminpassword);
        if (list.size()==1){
            return true;
        }
        return false;
    }

    @Override
    public List<User> findAllUser() {
        String sql = "select * from user";
        return jt.query(sql,new BeanPropertyRowMapper<>(User.class));
    }

    @Override
    public void deleteUserByUid(int uid) {
        String sql = "delete from user where uid = ?";
        jt.update(sql,uid);
    }

    @Override
    public void updateComment(int uid) {
        String sql = "update user set commentstatus = 'false' where uid = ?";
        jt.update(sql,uid);
    }

    @Override
    public int findCount() {
        String sql = "select count(*) from user";
        return jt.queryForObject(sql,Integer.class);
    }

    @Override
    public List<User> findByPage(int start, int pageSize) {
        String sql = "select * from user limit ? , ?";
        return jt.query(sql,
                new BeanPropertyRowMapper<>(User.class),start,pageSize);
    }

    @Override
    public void deleteFavByUid(int uid) {
        String sql = "delete from favorite where uid = ?";
        jt.update(sql,uid);
    }

    @Override
    public void deleteComByUid(int uid) {
        String sql = "delete from comment where uid = ?";
        jt.update(sql,uid);
    }

    @Override
    public void open(int uid) {
        String sql = "update user set commentstatus = 'true' where uid = ?";
        jt.update(sql,uid);
    }

    @Override
    public List<Favorite> findFavByUid(int uid) {
        String sql ="select * from favorite where uid = ?";
        return jt.query(sql,new BeanPropertyRowMapper<>(Favorite.class),uid);
    }
}
